# 通过手动输入的描述，生成对应的用例
import streamlit as st
from large_model.agent_executors import Executors
from streamlit_model.streamlit_utils.session_utils import load_session, save_session

PAGE_KEY = "description_case"

def description_case_page():
    # 1. 加载本页面缓存
    cached = load_session(PAGE_KEY)

    # 2. 清除标记
    if st.session_state.pop("clear_triggered", False):
        st.session_state.clear()
        st.rerun()

    if "task_executed" not in st.session_state:
        st.session_state.update(cached)

    # 3. 已生成则展示
    if st.session_state.get("task_executed"):
        _, center_output, _ = st.columns([1, 3, 1])
        with center_output:
            st.success("测试用例生成完成！")
            st.markdown(st.session_state["output"])

    default_description = cached.get("description", "需要编写测试用例的功能或者需求:")
    default_review_method = cached.get("review_method", "direct")

    st.text("通过手动输入的描述，生成对应的用例")

    _, select_col, _ = st.columns([1, 3, 1])
    with select_col:
        review_method_label = st.selectbox(
            "请选择生成测试用例的方式",
            ["直接生成用例", "需要二次评审"],
            index=0 if default_review_method == "direct" else 1
        )
        review_method = "direct" if review_method_label == "直接生成用例" else "review"

    _, center_col, _ = st.columns([1, 3, 1])
    with center_col:
        description = st.text_area(
            "请输入描述",
            height=500,
            value=default_description,
            max_chars=3000,
            placeholder="请输入你要生成测试用例的需求描述和对应的要求"
        )

    # 4. 交互后即时保存
    need_save = (
        description != cached.get("description") or
        review_method != cached.get("review_method")
    )
    if need_save and not st.session_state.get("clear_triggered", False):
        save_session(PAGE_KEY, {
            "description": description,
            "review_method": review_method,
            "task_executed": st.session_state.get("task_executed", False),
            "output": st.session_state.get("output", "")
        })

    # 5. 按钮行
    _, btn_col, _ = st.columns([2, 1, 2])
    with btn_col:
        generate_button = st.button("生成用例", use_container_width=True)
        if st.button("清除缓存", use_container_width=True):
            save_session(PAGE_KEY, {})
            st.session_state["clear_triggered"] = True
            st.rerun()

        # 6. 生成逻辑
        if generate_button and description.strip():
            executors = Executors()
            with st.spinner("正在生成测试用例..."):
                output = (
                    executors.description_main(description)
                    if review_method == "direct"
                    else executors.case_main(description)
                )

            session_data = {
                "description": description,
                "review_method": review_method,
                "task_executed": True,
                "output": output
            }
            st.session_state.update(session_data)
            if not st.session_state.get("clear_triggered", False):
                save_session(PAGE_KEY, session_data)
            st.rerun()